﻿namespace SERFLITEMAX20.Viewer
{
    using SERFLITEMAX20;
    using SERFLITEMAX20.DataObjects;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    public class MyFilterList : UserControl
    {
        private Button btnFind;
        private ColumnHeader columnHeader1;
        private ColumnHeader columnHeader2;
        private ColumnHeader columnHeader3;
        private ColumnHeader columnHeader4;
        private ColumnHeader columnHeader5;
        private ColumnHeader columnHeader6;
        private IContainer components = null;
        private ListView lstView;
        private IList<string> mFilterList = null;
        private ListViewSortManager mListViewSortManager = null;
        private ContextMenuStrip mnuMain;
        private ToolStripMenuItem mnuMainAssignToGroup;
        private ToolStripMenuItem mnuMainClearFiltered;
        private ToolStripMenuItem mnuMainClearSpecial;
        private ToolStripMenuItem mnuMainSetFiltered;
        private ToolStripMenuItem mnuMainSetSpecial;
        private Panel panel1;
        private TextBox txtFindText;

        public MyFilterList()
        {
            this.InitializeComponent();
        }

        private void AddItem(FilterObject pFilterObject)
        {
            if (pFilterObject.name.Length > 0)
            {
                ListViewItem item = this.lstView.Items.Add(pFilterObject.entry.ToString());
                item.Tag = pFilterObject.key;
                item.SubItems.Add(pFilterObject.name);
                item.SubItems.Add(pFilterObject.type);
                item.SubItems.Add(pFilterObject.typeid);
                item.SubItems.Add(pFilterObject.filtered.ToString());
                item.SubItems.Add(pFilterObject.special.ToString());
                if (pFilterObject.filtered)
                {
                    item.ForeColor = Color.Red;
                }
                else if (pFilterObject.special)
                {
                    item.ForeColor = Color.Green;
                }
            }
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            if (this.txtFindText.Text.Length > 0)
            {
                this.lstView.ListViewItemSorter = null;
                this.InitListViewSorter();
                this.lstView.Items.Clear();
                if (this.mFilterList != null)
                {
                    foreach (string str in this.mFilterList)
                    {
                        if (Support.filterObjectList.ContainsKey(str) && (Support.filterObjectList[str].name.IndexOf(this.txtFindText.Text, StringComparison.CurrentCultureIgnoreCase) != -1))
                        {
                            this.AddItem(Support.filterObjectList[str]);
                        }
                    }
                    this.lstView.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                    this.lstView.Columns[1].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                    this.lstView.Columns[2].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                    this.lstView.Columns[3].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                    this.lstView.Columns[4].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                    this.lstView.Columns[5].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                }
            }
            else
            {
                this.lstView.ListViewItemSorter = null;
                this.InitListViewSorter();
                this.lstView.Items.Clear();
                this.Initialize(this.mFilterList);
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        public void Initialize(IList<string> pFilterList)
        {
            this.lstView.ListViewItemSorter = null;
            this.mListViewSortManager = null;
            this.lstView.Items.Clear();
            this.mFilterList = pFilterList;
            if (pFilterList != null)
            {
                foreach (string str in pFilterList)
                {
                    if (Support.filterObjectList.ContainsKey(str))
                    {
                        this.AddItem(Support.filterObjectList[str]);
                    }
                }
                this.lstView.Columns[0].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                this.lstView.Columns[1].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                this.lstView.Columns[2].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                this.lstView.Columns[3].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                this.lstView.Columns[4].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                this.lstView.Columns[5].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
            }
            else
            {
                this.lstView.Items.Clear();
            }
            this.InitListViewSorter();
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            this.lstView = new ListView();
            this.columnHeader4 = new ColumnHeader();
            this.columnHeader1 = new ColumnHeader();
            this.columnHeader2 = new ColumnHeader();
            this.columnHeader3 = new ColumnHeader();
            this.columnHeader5 = new ColumnHeader();
            this.columnHeader6 = new ColumnHeader();
            this.mnuMain = new ContextMenuStrip(this.components);
            this.mnuMainSetFiltered = new ToolStripMenuItem();
            this.mnuMainSetSpecial = new ToolStripMenuItem();
            this.mnuMainClearFiltered = new ToolStripMenuItem();
            this.mnuMainClearSpecial = new ToolStripMenuItem();
            this.mnuMainAssignToGroup = new ToolStripMenuItem();
            this.panel1 = new Panel();
            this.txtFindText = new TextBox();
            this.btnFind = new Button();
            this.mnuMain.SuspendLayout();
            this.panel1.SuspendLayout();
            base.SuspendLayout();
            this.lstView.Columns.AddRange(new ColumnHeader[] { this.columnHeader4, this.columnHeader1, this.columnHeader2, this.columnHeader3, this.columnHeader5, this.columnHeader6 });
            this.lstView.ContextMenuStrip = this.mnuMain;
            this.lstView.Dock = DockStyle.Fill;
            this.lstView.FullRowSelect = true;
            this.lstView.HideSelection = false;
            this.lstView.Location = new Point(0, 0x22);
            this.lstView.Name = "lstView";
            this.lstView.Size = new Size(0x30b, 0x1a5);
            this.lstView.TabIndex = 1;
            this.lstView.UseCompatibleStateImageBehavior = false;
            this.lstView.View = View.Details;
            this.columnHeader4.Text = "Entry";
            this.columnHeader4.Width = 0x80;
            this.columnHeader1.Text = "Name";
            this.columnHeader1.Width = 0x62;
            this.columnHeader2.Text = "Type";
            this.columnHeader2.Width = 0x62;
            this.columnHeader3.Text = "TypeID";
            this.columnHeader3.Width = 0x6b;
            this.columnHeader5.Text = "filtered";
            this.columnHeader6.Text = "special";
            this.mnuMain.Items.AddRange(new ToolStripItem[] { this.mnuMainSetFiltered, this.mnuMainSetSpecial, this.mnuMainClearFiltered, this.mnuMainClearSpecial, this.mnuMainAssignToGroup });
            this.mnuMain.Name = "contextMenuStrip1";
            this.mnuMain.Size = new Size(0xa4, 0x72);
            this.mnuMainSetFiltered.Name = "mnuMainSetFiltered";
            this.mnuMainSetFiltered.Size = new Size(0xa3, 0x16);
            this.mnuMainSetFiltered.Text = "Set Filtered";
            this.mnuMainSetFiltered.Click += new EventHandler(this.mnuMainSetFiltered_Click);
            this.mnuMainSetSpecial.Name = "mnuMainSetSpecial";
            this.mnuMainSetSpecial.Size = new Size(0xa3, 0x16);
            this.mnuMainSetSpecial.Text = "Set Special";
            this.mnuMainSetSpecial.Click += new EventHandler(this.mnuMainSetSpecial_Click);
            this.mnuMainClearFiltered.Name = "mnuMainClearFiltered";
            this.mnuMainClearFiltered.Size = new Size(0xa3, 0x16);
            this.mnuMainClearFiltered.Text = "Clear Filtered";
            this.mnuMainClearFiltered.Click += new EventHandler(this.mnuMainClearFiltered_Click);
            this.mnuMainClearSpecial.Name = "mnuMainClearSpecial";
            this.mnuMainClearSpecial.Size = new Size(0xa3, 0x16);
            this.mnuMainClearSpecial.Text = "Clear Special";
            this.mnuMainClearSpecial.Click += new EventHandler(this.mnuMainClearSpecial_Click);
            this.mnuMainAssignToGroup.Name = "mnuMainAssignToGroup";
            this.mnuMainAssignToGroup.Size = new Size(0xa3, 0x16);
            this.mnuMainAssignToGroup.Text = "Assign To Group";
            this.mnuMainAssignToGroup.Click += new EventHandler(this.mnuMainAssignToGroup_Click);
            this.panel1.Controls.Add(this.btnFind);
            this.panel1.Controls.Add(this.txtFindText);
            this.panel1.Dock = DockStyle.Top;
            this.panel1.Location = new Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new Size(0x30b, 0x22);
            this.panel1.TabIndex = 3;
            this.txtFindText.Location = new Point(0x3d, 8);
            this.txtFindText.Name = "txtFindText";
            this.txtFindText.Size = new Size(0x9a, 20);
            this.txtFindText.TabIndex = 0;
            this.btnFind.Location = new Point(0xed, 5);
            this.btnFind.Name = "btnFind";
            this.btnFind.Size = new Size(0x75, 0x18);
            this.btnFind.TabIndex = 1;
            this.btnFind.Text = "Find";
            this.btnFind.UseVisualStyleBackColor = true;
            this.btnFind.Click += new EventHandler(this.btnFind_Click);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            
            base.Controls.Add(this.lstView);
            base.Controls.Add(this.panel1);
            base.Name = "MyFilterList";
            base.Size = new Size(0x30b, 0x1c7);
            this.mnuMain.ResumeLayout(false);
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            base.ResumeLayout(false);
        }

        private void InitListViewSorter()
        {
            this.mListViewSortManager = new ListViewSortManager(this.lstView, new System.Type[] { typeof(ListViewInt32Sort), typeof(ListViewTextSort), typeof(ListViewTextSort), typeof(ListViewTextSort), typeof(ListViewTextSort), typeof(ListViewTextSort) });
        }

        private void mnuMainAssignToGroup_Click(object sender, EventArgs e)
        {
        }

        private void mnuMainClearFiltered_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in this.lstView.SelectedItems)
            {
                string tag = (string) item.Tag;
                if (Support.filterObjectList.ContainsKey(tag))
                {
                    item.SubItems[4].Text = false.ToString();
                    Support.filterObjectList[tag].filtered = false;
                    if (Support.filterObjectList[tag].special)
                    {
                        item.ForeColor = Color.Green;
                    }
                    else
                    {
                        item.ForeColor = SystemColors.WindowText;
                    }
                }
            }
        }

        private void mnuMainClearSpecial_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in this.lstView.SelectedItems)
            {
                string tag = (string) item.Tag;
                if (Support.filterObjectList.ContainsKey(tag))
                {
                    item.SubItems[5].Text = false.ToString();
                    Support.filterObjectList[tag].special = false;
                    if (!Support.filterObjectList[tag].filtered)
                    {
                        item.ForeColor = SystemColors.WindowText;
                    }
                }
            }
        }

        private void mnuMainSetFiltered_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in this.lstView.SelectedItems)
            {
                string tag = (string) item.Tag;
                if (Support.filterObjectList.ContainsKey(tag))
                {
                    item.SubItems[4].Text = true.ToString();
                    Support.filterObjectList[tag].filtered = true;
                    item.ForeColor = Color.Red;
                }
            }
        }

        private void mnuMainSetSpecial_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in this.lstView.SelectedItems)
            {
                string tag = (string) item.Tag;
                if (Support.filterObjectList.ContainsKey(tag))
                {
                    item.SubItems[5].Text = true.ToString();
                    Support.filterObjectList[tag].special = true;
                    if (item.ForeColor != Color.Red)
                    {
                        item.ForeColor = Color.Green;
                    }
                }
            }
        }
    }
}

